<?php
/**
 * @title 1123. 最深叶节点的最近公共祖先
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function lcaDeepestLeaves($root) {
        /**
         * @since 2023-09-06 找到最深的叶子节点
         */
        $leafs = $levels = [];
        $nodes = [$root];
        while(true){
            $levels[] = $nodes;
            $nextNodes = [];

            /**
             * @since 2023-09-06 查找下一层
             */
            foreach ($nodes as $node){
                /**
                 * @since 2023-09-06 左子树
                 */
                if($node->left !== null){
                    $nextNodes[] = $node->left;
                } else {}

                /**
                 * @since 2023-09-06 右子树
                 */
                if($node->right !== null){
                    $nextNodes[] = $node->right;
                } else {}
            }

            /**
             * @since 2023-09-06 最深一层
             */
            if(empty($nextNodes)){
                $leafs = $nodes;
                break;
            } else {
                $nodes = $nextNodes;
            }
        }

        /**
         * @since 2023-09-06 只有一个
         */
        if(count($leafs) == 1){
            return $leafs[0];
        } else {}

        /**
         * @since 2023-09-06 所有叶子节点值
         */
        $vals = [];
        foreach ($leafs as $leaf){
            $vals[] = $leaf->val;
        }

        /**
         * @since 2023-09-06 查找共同的祖先节点
         */
        for($n=count($levels)-2; $n>=0; $n--){
            $nextVals = [];
            foreach ($levels[$n] as $node){
                /**
                 * @since 2023-09-06 左子树节点
                 */
                if($node->left !== null){
                    if(in_array($node->left->val, $vals) !== false){
                        $nextVals[] = $node->val;
                    } else {}
                } else {}

                /**
                 * @since 2023-09-06 右子树节点
                 */
                if($node->right !== null){
                    if(in_array($node->right->val, $vals) !== false){
                        $nextVals[] = $node->val;
                    } else {}
                } else {}
            }

            /**
             * @since 2023-09-06 去重
             */
            $nextVals = array_unique($nextVals);

            /**
             * @since 2023-09-06 只有一个节点，找到
             */
            if(count($nextVals) == 1){
                $val = $nextVals[0];

                foreach ($levels[$n] as $node){
                    if($node->val == $val){
                        /**
                         * @return
                         */
                        return $node;
                    } else {}
                }

                break;
            } else {}

            /**
             * @since 2023-09-06 下次查找
             */
            $vals = $nextVals;
        }
    }
}



/**
 * @url http://127.0.0.1/leetcode/202309/2023.09.06.php
 */
$datas = [
    [3,5,1,6,2,0,8,null,null,7,4],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    // $result = $obj->(...$data);
    $result = $obj->lcaDeepestLeaves($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
